package presentation;

import javax.servlet.http.HttpSession;

import metier.UserMetier;
import modele.Administrateur;
import modele.User;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private static final long serialVersionUID = 17666186436829318L;

	private String login;
	private String password;

	public String verifierUtilisateur() {
		User user = UserMetier.verifierUtilisateur(login, password);
		if (user != null) {
			HttpSession session = ServletActionContext.getRequest()
					.getSession();
			session.setAttribute("identifiant", user.getIdentifiant());
			session.setAttribute("nom", user.getNom());
			session.setAttribute("prenom", user.getPrenom());
			if (user instanceof Administrateur) {
				session.setAttribute("type", "Administrateur");
				return "successAdministrateur";
			} 
		}
		addActionMessage("Nom d'utilisateur et/ou le mot de passes sont incorrects!");
		return Action.LOGIN;
	}

	public String seDeconnecter() {
		ServletActionContext.getRequest().getSession().invalidate();
		return Action.LOGIN;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
